package cn.ruituo.adv9;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;
import java.util.Scanner;

public class XMLSearch2 {
    public static void main(String[] args) {

        //超过某个价格的书籍信息
        SAXReader reader = new SAXReader();
        //相对目录
        Document doc = null;
        try {
            doc = reader.read(new File("advance/src/cn/cn.ruituo/books.xml"));
        } catch (DocumentException e) {
            System.out.println("解析失败");
            e.printStackTrace();
            return;
        }
        Scanner in = new Scanner(System.in);
        System.out.println("请输入要查找的价格：");
        float sprice = in.nextFloat();
        List<Element> list = doc.selectNodes("/books/book[price>" + sprice + "]");
        if (list.size() == 0) {
            System.out.println("没有");
            return;
        }
        System.out.println("对应的book信息如下:");
        for (Element e : list) {
            //获得属性
            String id = e.attributeValue("id");
            String diff = e.attributeValue("diff");
            //String->Integer
            //int bookid=Integer.parseInt(id);
            //Element ename= (Element) e.selectNodes("name").get(0);
            //确定最多一个name子节点
            Element ename = (Element) e.selectSingleNode("name");
            String name = ename.getText();
            Element eprice = (Element) e.selectSingleNode("price");
            String price = eprice.getText();
            Element epublishi = (Element) e.selectSingleNode("publish");
            String publish = epublishi.getText();
            Element edesc = (Element) e.selectSingleNode("description");
            String desc = edesc.getText().trim();
            System.out.printf("id=%s,diff=%s,价格=%s,name=%s,publish=%s,描述=%s\n", id, diff,price, name, publish, desc);
        }
    }
}

